using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.GUI;
using GarageGames.Torque.Sim;
using Xyne.Utilities;
using Xyne.Events;
using Microsoft.Xna.Framework;
using GarageGames.Torque.Platform;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Xyne.GUI
{
    class EndGameGUI : GUIControl, IGUIScreen
    {
        #region Variables
        GUIBitmap _backgroundBitmap;
        GUIText _endGameMenuText;
        #endregion

        public String EndGameText
        {
            get { return _endGameMenuText.Text; }
            set { _endGameMenuText.Text = value; }
        }

        #region Constructor
        public EndGameGUI()
        {
            GUIStyle stylePlayGUI = new GUIStyle();

            Name = "EndGameMenu";
            Style = stylePlayGUI;
            Size = Game.Instance.ClientBounds;

            GUIBitmapStyle bitmapStyle = new GUIBitmapStyle();
            bitmapStyle.IsOpaque = false;
            _backgroundBitmap = new GUIBitmap();
            _backgroundBitmap.Bitmap = @"data\images\ui\card\cardbackground";
            _backgroundBitmap.Style = bitmapStyle;
            _backgroundBitmap.Position = new Vector2(0, 0);
            _backgroundBitmap.Size = Size;
            _backgroundBitmap.Visible = false;
            _backgroundBitmap.Opacity = 0.5f;

            _backgroundBitmap.Folder = this;

            _endGameMenuText = new GUIText();
            GUITextStyle textStyle = new GUITextStyle();
            textStyle.FontType = "Arial14";
            textStyle.SizeToText = true;
            textStyle.TextColor[CustomColor.ColorBase] = Color.Black;
            _endGameMenuText.Style = textStyle;
            _endGameMenuText.Position = new Vector2(100, 300);
            _endGameMenuText.Visible = false;
            _endGameMenuText.Folder = this;
            OnGUIWake = new OnWakeDelegate(StartEndGameGUI);
        }
        #endregion

        #region Events

        public void StartEndGameGUI(GUIControl control)
        {


        }

        private void OnButtonPress(float val)
        {
            if (val > 0)
            {
                Game.Network.CloseSession();
                Game.Instance.SceneLoader.OnSceneLoaded = null;
                
                CanvasStateMachine.Instance.NextCanvasState = CanvasStateMachine.CanvasStates.MainMenu;
            }
        }
        #endregion

        #region Methods
        public void Init()
        {
            _backgroundBitmap.Visible = false;
            _endGameMenuText.Visible = false;
        }
        public void EnableMenu()
        {
            _backgroundBitmap.Visible = true;
            _endGameMenuText.Visible = true;
            InputMap inputMap = PlayerManager.Instance.GetPlayer(0).InputMap;
            int keyboardId = InputManager.Instance.FindDevice("keyboard");
            if (keyboardId >= 0)
            {
                inputMap.BindAction(keyboardId, (int)Keys.Escape, OnButtonPress);

                inputMap.BindAction(keyboardId, (int)Keys.P, OnButtonPress);
            }
            int gamepadId = InputManager.Instance.FindDevice("gamepad0");
            if (gamepadId >= 0)
            {
                inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Start, OnButtonPress);
            }
        }
        #endregion
    }
}
